package com.zpxt.zpxt.controller;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.zpxt.zpxt.entity.Company;
import com.zpxt.zpxt.entity.Talent;
import com.zpxt.zpxt.serve.AccountServe;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author:hjx
 * @Description:账户相关
 * @Date: Created in 20:24 2018/4/11
 * @Modified By:
 */
@CrossOrigin
@RestController
public class AccountController {
    @Resource(name="accountServe")
    AccountServe accountServe;
    Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();

//  找回密码
    @RequestMapping("/findPassword/company")
    @ResponseBody
    public String findPasswordInCompany(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String code = request.getParameter("code");
        accountServe.findPasswordInComputer(code);
        return gson.toJson("OK");
    }

    @RequestMapping("/findPassword/talent")
    @ResponseBody
    public String findPasswordInTalent(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String code = request.getParameter("code");
        accountServe.findPasswordInTalent(code);
        return gson.toJson("OK");
    }
//  修改密码
    @RequestMapping("/editPassword/company")
    @ResponseBody
    public String editCompanyPassword(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String code = request.getParameter("code");
        String password = request.getParameter("password");
        String newPassword = request.getParameter("newPassword");

        accountServe.editCompanyPassword(code,password,newPassword);

        return gson.toJson("OK");
    }

    @RequestMapping("/editPassword/talent")
    @ResponseBody
    public String editTalentPassword(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String code = request.getParameter("code");
        String password = request.getParameter("password");
        String newPassword = request.getParameter("newPassword");

        accountServe.editTalentPassword(code,password,newPassword);

        return gson.toJson("OK");
    }
//  查找信息
    @RequestMapping("/findInfo/company")
    @ResponseBody
    public Company findInfoCompany(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String code = request.getParameter("code");

        Company company = accountServe.findInfoCompany(code);

        return company;
    }

    @RequestMapping("/findInfo/talent")
    @ResponseBody
    public Talent findInfoTalent(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String code = request.getParameter("code");

        Talent talent = accountServe.findInfoTalent(code);

        return talent;
    }
//    修改资料
@RequestMapping("/editCompany")
@ResponseBody
public String addCompany(HttpServletRequest request, HttpServletResponse response) throws Exception {

    String info = request.getParameter("data");
    Company company=gson.fromJson(info, Company.class);

    //添加企业信息
    accountServe.editCompany(company);

    //返回结果
    return gson.toJson("OK");
}

    @RequestMapping("/editTalent")
    @ResponseBody
    public String addTalent(HttpServletRequest request, HttpServletResponse response) throws Exception {

        String info = request.getParameter("data");
        Talent talent=gson.fromJson(info, Talent.class);

        //添加个人信息
        accountServe.editTalent(talent);

        //返回结果
        return gson.toJson("OK");
    }
}
